import IButtonOptions from "@/components/ButtonGroup/button-scheme";
import InputType from '@/enum/InputType'
import {IFormConfig, IFormItem, ISelectOption} from "@/components/Form/form-scheme";
import {getAllTask} from "@/api/sysscheduledApi";

/**
 * 查询表单配置
 */
const searchItems: IFormItem[] =  [
            {
                model: 'scheduledName',
                type: InputType.INPUT,
                label: `任务名:`,
                clearable: true
            },
]
const SearchConfig: IFormConfig = {
    items: searchItems,
    labelWidth: "80px",
    span: 6
}
export {SearchConfig}

/**
 * 按钮组配置
 */
const ButtonConfig: IButtonOptions = {
    reset: {
        clickCall: 'reset',
        btnTip: '重置'
    },
    query: {
        clickCall: 'query',
        btnTip: '查询'
    },
    add: {
        clickCall: 'openDialog',
        btnTip: '添加'
    },
    edit: {
        clickCall: 'openDialog',
        btnTip: '编辑'
    },
    delete: {
        clickCall: 'delete',
        btnTip: '删除'
    }
}
export {ButtonConfig}


/**
 * 添加或更新表单配置
 */
let taskGroup: ISelectOption[] = []
let task: ISelectOption[] = []
let taskGroups:any = []
let tasks:any = []
function initOptions() {
    getAllTask().then(data => {
        if(data && data instanceof Array) {
            taskGroups = data
            data.forEach(item => {
                taskGroup.push({
                    title: item.taskGroupName,
                    value: item.beanId
                })
            })
        }
    })
}
export {initOptions}

const formItems: IFormItem[] = [
    {
        model: 'beanId',
        type: InputType.SELECT,
        placeholder: '-- 请选择 --',
        label: `任务组:`,
        clearable: true,
        options: taskGroup,
        ruleName: 'beanId',
        onchange: function (val) {
            taskGroups.forEach(item => {
                if (item.beanId === val) {
                    tasks = item.tasks
                    if (tasks) {
                        tasks.forEach(i => {
                            task.push({
                                title: i.scheduledName,
                                value: i.id
                            })
                        })
                    }
                    return
                }
            })
        },
        onclear: function () {
            FormData.id = null
            FormData.scheduledName = null
            FormData.methodName = null
        }
    },
    {
        model: 'id',
        type: InputType.SELECT,
        placeholder: '-- 请选择 --',
        label: `任务标识:`,
        clearable: true,
        options: task,
        ruleName: 'id',
        onchange: function (val) {
            tasks.forEach(item => {
                if(val === item.id) {
                    FormData.methodName = item.methodName
                    FormData.scheduledName = item.scheduledName
                    return
                }
            })
        },
        onclear: function () {
            FormData.methodName = null
            FormData.scheduledName = null
        }
    },
    {
        model: 'scheduledName',
        type: InputType.INPUT,
        label: `任务名:`,
        clearable: true,
        disable: true
    },
    {
        model: 'cron',
        type: InputType.CRON,
        label: `执行周期:`,
        clearable: true,
        ruleName: 'cron'
    },
    {
        model: 'methodName',
        type: InputType.INPUT,
        label: `方法名:`,
        clearable: true,
        disable: true
    },
    {
        model: 'methodParam',
        type: InputType.INPUT,
        label: `任务参数:`,
        clearable: true
    },
    {
        model: 'remark',
        type: InputType.INPUT,
        label: `备注:`,
        clearable: true
    },
]
const FormConfig: IFormConfig = {
    items: formItems,
    labelWidth: '120px',
    span: 12,
    rules: {
        beanId: [
            {required: true,message: '请选择任务组',trigger: 'change'}
        ],
        id: [
            {required: true,message: '请选择任务',trigger: 'change'}
        ],
        cron: [
            {required: true,message: '请配置执行周期',trigger: 'blur'}
        ]
    }
}
let FormData =  {
    id: null,
    scheduledName: null,
    cron: null,
    beanId: null,
    methodName: null,
    methodParam: null,
    remark: null,
}
export {FormConfig,FormData}